// 引入获取用户信息的接口请求
import { userInfo } from '@/apis/personal'

export default {
  namespaced: true,
  state: {
    token: '', // 登录之后返回的token
    userInfo: {} // 用来接收用户信息
  },
  getters: {
    isLogin: (state) => {
      return !!state.token || !!localStorage.getItem('pm_token')
    }
  },
  mutations: {
    // 设置token
    setToken(state, token) {
      localStorage.setItem('pm_token', token)
      state.token = token
    },
    // 清除token
    clearToken(state) {
      state.token = ''
      localStorage.removeItem('pm_token')
    },
    // 设置用户信息
    setUserInfo(state, info) {
      state.userInfo = info || {}
    },
    // 清除用户信息
    clearUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    // 获取用户信息
    refreshInfo({ commit }) {
      userInfo().then((res) => {
        console.log('用户的信息是', res);
        commit('setUserInfo', res.data)
      })
    }
  }
}